package com.ibm.ecm.configmgr.app.acm.widgets.rest;

import java.io.File;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

import com.ibm.ecm.configmgr.app.acm.widgets.util.XMLUtils;

public class RESTContentReader {
//	public static String getContent(String filename) throws IOException{
//		InputStream in = RESTContentReader.class.getResourceAsStream(filename);
//		InputStreamReader inReader = new InputStreamReader(in, "UTF-8");
//		BufferedReader reader = new BufferedReader(inReader);
//
//		String line = "";
//		String content = "";
//		while ((line = reader.readLine()) != null) {
//			line=line.replaceAll("\"", "\\\"").trim();
//			content=content+line;
//		}
//		reader.close();
//		//System.out.println(content);
//		return content;
//	}
    static String catalogFile ="";
    static String hostname ="localhost";
    static String port = "9443";
	static String acmwidgetsHome;
	
	public static ArrayList<String> getATOMAddWidgetsinHUB(String widgetAuthor) throws Exception{
		InputStream in = RESTContentReader.class.getResourceAsStream("/com/ibm/ecm/configmgr/app/acm/widgets/rest/AddWidgetsInHUB.xml");
		XMLUtils util= new XMLUtils(in);
		String widgetName="";
		String widgetDesc="";
		String widgetURL="";
		System.out.println(transform(util.getDocument()));
		ArrayList al = CatalogReader.getCatalog(RESTContentReader.catalogFile);
		ArrayList atoms = new ArrayList();
		for(int i = 0; i<al.size(); i++){
			HashMap<String, String> hashMap = (HashMap<String, String>)al.get(i);
			widgetName = hashMap.get("WIDGET_NAME").toString();
			util.updateDocument("//catalog:name", widgetName);
//			widgetAuthor = hashMap.get("WIDGET_AUTHOR").toString();
			util.updateDocument("//catalog:author", widgetAuthor);
			widgetDesc = hashMap.get("WIDGET_DESC").toString();
			util.updateDocument("//catalog:description", widgetDesc);
			//TODO: Widgets URL is hard code to localhost now. Need to improve later
			widgetURL = "https://"+hostname+":"+port+"/ACMWidgets/"+hashMap.get("WIDGET_URL").toString();
			util.updateDocument("//catalog:definitionURL", widgetURL);
			//System.out.println(transform(util.getDocument()));
			atoms.add(transform(util.getDocument()));
		}

		return atoms;
	}
	
	public static ArrayList<String> getATOMAddWidgetsinPalette() throws Exception{
		String atomSource = acmwidgetsHome+File.separator+"catalog"+File.separator+"atom"+File.separator+"mm";
		File atomFolder = new File(atomSource);
		File[] files = atomFolder.listFiles();
		XMLUtils util;
		String widgetName="";
		String widgetDesc="";
		String widgetURL="";
		String widgetIconURL="";

		ArrayList atoms = new ArrayList();
		for(int i = 0; i<files.length; i++){
            util = new XMLUtils(files[i]);
			atoms.add(transform(util.getDocument()));
		}

		return atoms;
	}
	public static String transform(Document doc) throws TransformerException{
	       DOMSource domSource = new DOMSource(doc);
	       StringWriter writer = new StringWriter();
	       StreamResult result = new StreamResult(writer);
	       TransformerFactory tf = TransformerFactory.newInstance();
	       Transformer transformer = tf.newTransformer();
	       transformer.transform(domSource, result);
	       String atomString = writer.toString();
	       atomString = atomString.replaceAll("\r\n", "");
	       atomString = atomString.replaceAll("\\t", "");
	       atomString = atomString.trim();
	       return atomString;
	}
	
	public static void setProperties(Properties props){
		if(props.get("CATALOG_FILE")!=null){
			catalogFile=props.get("CATALOG_FILE").toString();
		}
		if(props.get("HOSTNAME")!=null){
		    hostname =props.get("HOSTNAME").toString();
		}
		if(props.get("PORT")!=null){
		    port = props.get("PORT").toString();
		}
		if(props.get("ACMWIDGETS_HOME")!=null){
		    acmwidgetsHome = props.get("ACMWIDGETS_HOME").toString();
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Properties prop = new Properties();
		prop.setProperty("ACMWIDGETS_HOME", "C:\\Program Files\\IBM\\ACM\\ACMWidgets");
		RESTContentReader.setProperties(prop);
		
		try {
			RESTContentReader.getATOMAddWidgetsinPalette();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
